package com.wenwu.controller.competence;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.Function;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.service.FunctionService;

/**
 * @project ATHome-Manager
 * @class FunctionController.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年3月3日 下午4:27:37
 * @description 页面功能按钮模块
 */
@Controller(value = "FunctionController")
@RequestMapping(value = "/function")
public class FunctionController {
	@Autowired
	private FunctionService functionService;
	Logger log = Logger.getLogger(this.getClass());
	// 用来创建json数据
	private JsonGenerator jsonGenerator = null;
	// 进入页面
	@RequestMapping(value = "/mgr/init")
	public String initPage() {
		log.info("进入功能模块页面...");
		return "function/functionlist";
	}

	@RequestMapping(value = "/mgr/functionform")
	public String functionform() {
		return "function/functionform";
	}

	// 进入功能选择页面
	@RequestMapping(value = "/mgr/functionselect")
	public String functionselect() {
		return "function/functionselect";
	}

	// 查询function数据
	@RequestMapping(value = "/mgr/showList")
	public void showList(Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			PaginationVO pageVo = PageUtils.getPaginationInfo(request);
			List<Function> list = functionService.selectList();// 查询符合条件的数据
			pageVo.setList(list);// 将数据设置到PaginationVO对象中
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}

	/**
	 * 验证action是否已经存在
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/mgr/veriKey")
	public void veriKey(HttpServletResponse response,
			@RequestParam(required = false, value = "code") String code)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		int result = functionService.veriKey(code);
		if (result > 0) {
			jsonGenerator.writeObject("true");
		} else {
			jsonGenerator.writeObject("false");
		}
	}

	/**
	 * 增加
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/mgr/addFunction", method = { RequestMethod.POST })
	public void addFunction(Function function, HttpServletResponse response,HttpServletRequest request)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {		
			if (function.getFunctionId() == null) {
				 functionService.addFunction(function);
				jsonGenerator.writeObject("添加成功");
			} else {
				functionService.updateFunction(function);
				jsonGenerator.writeObject("修改成功");
			}
		} catch (Exception e) {
			jsonGenerator.writeObject("添加失败");
		}finally{
		}
	}

	/**
	 * 资源修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/mgr/showUpdate")
	public void showUpdate(
			HttpServletResponse response,
			@RequestParam(required = false, value = "functionId") Integer functionId)
			throws Exception {
		if (functionId != null) {
			Function function = functionService.selectByKey(functionId);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(function);
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/mgr/deleteFunction")
	public void deleteFunction(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("主键：" + key);
		if (key != null) {
			List<String> keys = new ArrayList<String>();
			String[] objs = key.split(",");
			for (String k : objs) {
				keys.add(k);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			try {
				int result = functionService.deleteFunctionByKey(keys);
				jsonGenerator.writeObject("成功删除" + result + "条数据");
			} catch (Exception e) {
				jsonGenerator.writeObject("删除数据失败");
			}
		}
	}
}
